package com.netrust.cmsx.core.persist;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

public class BaseDaoFactoryBean<R extends JpaRepository<T, I>, T,I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {
	public BaseDaoFactoryBean(Class<? extends R> repositoryInterface) {
		super(repositoryInterface);
	}

	@SuppressWarnings("rawtypes")
	@Override
	protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
		return new BaseDaoFactory(em);
	}
	
	//创建一个内部类，该类不用在外部访问
	private static class BaseDaoFactory<T, I extends Serializable> extends JpaRepositoryFactory {
		private final EntityManager em;
		
		public BaseDaoFactory(EntityManager em) {
		    super(em);
		    this.em = em;
		}
		
		//设置具体的实现类是BaseRepositoryImpl
		@SuppressWarnings("unchecked")
		@Override
		protected Object getTargetRepository(RepositoryInformation information) {
		    return new BaseDaoImpl<T, I>((Class<T>) information.getDomainType(), em);
		}
		
		//设置具体的实现类的class
		@Override
		protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
		    return BaseDaoImpl.class;
		}
	}
}